package com.nine.mymall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nine.mymall.product.vo.BrandVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.nine.mymall.product.entity.CategoryBrandRelationEntity;
import com.nine.mymall.product.service.CategoryBrandRelationService;
import com.nine.common.utils.PageUtils;
import com.nine.common.utils.R;



/**
 * 品牌分类关联
 *
 * @author nine
 * @email 2685441173@qq.com
 * @date 2021-08-27 17:57:46
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;


    /**
     * 获取分类关联的品牌
     * catId 必须传递
     *
     * 1、Controller：处理请求，接收和校验数据
     * 2、Service：接收 Controller 传来的数据，进行业务处理
     * 3、Controller：接收 Service 处理完的数据，封装成页面需要的 VO
     */
    @RequestMapping(value = "/brands/list", method = RequestMethod.GET)
    public R brandlist(@RequestParam(value = "catId", required = true) Long catId){
        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("catelog_id", catId);
        List<CategoryBrandRelationEntity> entities = categoryBrandRelationService.list(wrapper);

        List<BrandVo> brandVos = entities.stream().map((entity) -> {
            BrandVo brandVo = new BrandVo();
            BeanUtils.copyProperties(entity, brandVo);
            return brandVo;
        }).collect(Collectors.toList());

        return R.ok().put("data", brandVos);
    }


    /**
     * 获取品牌关联的分类
     */
    @RequestMapping(value = "/catelog/list", method = RequestMethod.GET)
    public R cateloglist(@RequestParam("brandId") Long brandId){
        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("brand_id", brandId);
        List<CategoryBrandRelationEntity> data = categoryBrandRelationService.list(wrapper);
        return R.ok().put("data", data);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("product:categorybrandrelation:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    // @RequiresPermissions("product:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存，新增品牌和分类的关联关系
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:categorybrandrelation:save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){

        categoryBrandRelationService.saveDetail(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
